package com.zqh.productskuquery.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqh.myfmmall.entity.ProductSku;
import com.zqh.myfmmall.entity.ShoppingCart;
import com.zqh.myfmmall.result.Result;
import com.zqh.productskuquery.dao.ProductSkuDao;
import com.zqh.productskuquery.service.ProductSkuQueryService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2022-02-18 2:19
 */
@Service("productSkuQueryService")
public class ProductSkuQueryServiceImpl extends ServiceImpl<ProductSkuDao, ProductSku> implements ProductSkuQueryService{

    @Override
    public Result getStockByIds(List<String> ids) {
        ArrayList<ProductSku> productSkus = new ArrayList<>();
        for (String id : ids) {
            ProductSku sku = query().eq("sku_id", id).select("sku_id", "stock").one();
            productSkus.add(sku);
        }
        return Result.success(productSkus);
    }

    @Override
    public Result getByPorductId(String productId) {
        List<ProductSku> skus = query().eq("product_id", productId).list();
        return Result.success(skus);
    }

    @Override
    public Result getProductsLowestPrice(List<String> productIds) {
        ArrayList<BigDecimal> lowestPrices = new ArrayList<>();
        for (String productId : productIds) {
            Page<ProductSku> page = new Page<>(1, 1);
            query().eq("product_id",productId).orderByDesc("sell_price").page(page);
            lowestPrices.add(page.getRecords().get(0).getSellPrice());
        }
        return Result.success(lowestPrices);

    }
}
